/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.misc;

import java.io.*;
import java.sql.*;
import nfse.*;

public class ConvertNotesToHex {

    public static void main(String[] args) {
        //while (true) {
            try {
                System.setProperty("nfse.configfile", args[0]);
                NetFSE.configure();

                String nodeConfigPath = args[1];
                File config2 = new File(nodeConfigPath);
                if (config2.exists()) {
                    NetFSE.processNodeConfigFile(config2);
                } else {
                    throw (new Exception("The specified node configuration file does not exist: '" + nodeConfigPath
                            + "'"));
                }

                JDBCDatabase resultDB = new JDBCDatabase("jdbc:mysql://" + NetFSE.RESULT_DB_HOST 
                        + "/" + NetFSE.RESULT_DB_DATABASE, NetFSE.RESULT_DB_USER, NetFSE.RESULT_DB_PASSWORD);

                Statement statement = resultDB.createStatement();
                Statement statement2 = resultDB.createStatement();
                
                String sql = "select * from Queries";
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next()) {
                    String queryID = rs.getString("Query_ID");
                    String note = rs.getString("Note");
                    boolean isHex = false;
                    try {
                        // See if the note is already hex
                        NetFSEUtil.convertFromHex(note);
                        isHex = true;
                    } catch (Exception e) {
                        
                    }
                    if (!isHex) {
                        note = NetFSEUtil.convertToHex(note);
                        sql = "update Queries set Note='" + note + "' where Query_ID='" + queryID + "'";
                        statement2.execute(sql);
                    }
                }
                rs.close();
                
                 sql = "select * from Query_Options";
                 rs = statement.executeQuery(sql);
                while (rs.next()) {
                    String queryID = rs.getString("Query_ID");
                    String note = rs.getString("Note");
                    if (note == null) {
                        note = "";
                        sql = "update Query_Options set Note='" + note + "' where Query_ID='" + queryID + "'";
                        statement2.execute(sql);
                    }
                    boolean isHex = false;
                    try {
                        // See if the note is already hex
                        NetFSEUtil.convertFromHex(note);
                        isHex = true;
                    } catch (Exception e) {
                        
                    }
                    if (!isHex) {
                        note = NetFSEUtil.convertToHex(note);
                        sql = "update Query_Options set Note='" + note + "' where Query_ID='" + queryID + "'";
                        statement2.execute(sql);
                    }
                }
                rs.close();
                
                statement.execute("use metadata");
                
                sql = "select * from Critical_Servers";
                 rs = statement.executeQuery(sql);
                while (rs.next()) {
                    String serverIP = rs.getString("Server_IP");
                    String note = rs.getString("Note");
                    if (note == null) {
                        note = "";
                        sql = "update Critical_Servers set Note='" + note + "' where Server_IP='" + serverIP + "'";
                        statement2.execute(sql);
                    }
                    boolean isHex = false;
                    try {
                        // See if the note is already hex
                        NetFSEUtil.convertFromHex(note);
                        isHex = true;
                    } catch (Exception e) {
                        
                    }
                    if (!isHex) {
                        note = NetFSEUtil.convertToHex(note);
                        sql = "update Critical_Servers set Note='" + note + "' where Server_IP='" + serverIP + "'";
                        statement2.execute(sql);
                    }
                }
                rs.close();
                statement.close();
                statement2.close();
                resultDB.closeConnection();
                

            } catch (Exception E) {
                E.printStackTrace();
            }
        //}
    }

}
